goog.provide('dkpoints.standings');

goog.require('goog.dom');
goog.require('soy');

goog.require('dkpoints.standings.templates');
goog.require('dkpoints.utils');
goog.require('dkpoints.rpc');

// Module name. Used by the Place manager.
dkpoints.standings.name = "/standings";

// Build up the UI
dkpoints.standings.loadUI = function() {  
  // Start up the RPC to retrieve the DKP list.
  // var getList = dkpoints.rpc.getDKPListJSON();
  
  var me = dkpoints.standings; // Alias for ease of use
  document.title = "DKPoints - DKP Standings";
  
  // Fake some Player DKP objects
  var raiders = [];
    raiders[0] = {rank: 1, plName: "Russ", plClass: "rogue", dkp: 25.6};
    raiders[1] = {rank: 2, plName: "Amaraz", plClass: "paladin", dkp: 25.5};
  
  // Fake a lot more.
  for(var i = 2; i < 15; ++i) {
    raiders[i] = {rank: i, plName: "Amaraz", plClass: "paladin", dkp: Math.round(100000*(25.5 - i - 0.3*i))/100000};
  }
  
  // Render the DKP list
  var body = goog.dom.$('body_content');
  var dkpList = goog.dom.createDom('div');
  dkpList.setAttribute('id', 'dkpTable');
  
  soy.renderElement(dkpList, dkpoints.standings.templates.dkpListing, {raiders: raiders});
  body.appendChild(dkpList);
};

// Remove any event listeners and dismantle DOM elements.
// @param preserveData - (boolean) true = Likely to return.  Don't destroy data.
dkpoints.standings.teardown = function(preserveData) {
  var me = dkpoints.standings;
  
  var list = goog.dom.$$('tbody', '', goog.dom.$('dkp_list'))[0];

  if(list && list.lastChild) {
    list.removeChild(list.lastChild);
    setTimeout(dkpoints.standings.teardown, 10);
  }
  else { // No?
    // Remove the DKP list container
    if(goog.dom.$('dkpTable')) {
      console.log('removing raider list');
      var panel =  goog.dom.$('dkpTable');
      panel.parentNode.removeChild(panel);
    }
  }
};